package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcComm;
import com.yxinsur.imc.business.domain.ImcCommDetails;
import com.yxinsur.imc.business.domain.ImcFund;
import com.yxinsur.imc.business.excel.CommDetailsTemplate;
import com.yxinsur.imc.business.form.CommDetailsForm;
import com.yxinsur.imc.business.mapper.ImcCommDetailsMapper;
import com.yxinsur.imc.business.mapper.ImcCommMapper;
import com.yxinsur.imc.business.mapper.ImcFundMapper;
import com.yxinsur.imc.business.service.IImcCommDetailsService;
import com.yxinsur.imc.business.vo.CommDetailsSearchVo;
import com.yxinsur.imc.common.annotation.Excel;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 佣金报主Service业务层处理
 *
 * @author imc
 * @date 2021-05-25
 */
@Service
public class ImcCommDetailsServiceImpl extends ServiceImpl<ImcCommDetailsMapper, ImcCommDetails> implements IImcCommDetailsService {
    @Autowired
    private ImcCommDetailsMapper imcCommDetailsMapper;
    @Autowired
    private ImcCommMapper imcCommMapper;
    @Autowired
    private ImcFundMapper imcFundMapper;

    /**
     * 查询佣金报主
     *
     * @param id 佣金报主ID
     * @return 佣金报主
     */
    @Override
    public ImcCommDetails selectImcCommDetailsById(Long id) {
        return imcCommDetailsMapper.selectImcCommDetailsById(id);
    }

    /**
     * 查询佣金报主列表
     *
     * @param imcCommDetails 佣金报主
     * @return 佣金报主
     */
    @Override
    public List<ImcCommDetails> selectImcCommDetailsList(ImcCommDetails imcCommDetails) {
        return imcCommDetailsMapper.selectImcCommDetailsList(imcCommDetails);
    }

    /**
     * 新增佣金报主
     *
     * @param imcCommDetails 佣金报主
     * @return 结果
     */
    @Override
    public int insertImcCommDetails(ImcCommDetails imcCommDetails) {
        imcCommDetails.setCreateTime(DateUtils.getNowDate());
        return imcCommDetailsMapper.insertImcCommDetails(imcCommDetails);
    }

    /**
     * 修改佣金报主
     *
     * @param imcCommDetails 佣金报主
     * @return 结果
     */
    @Override
    public int updateImcCommDetails(ImcCommDetails imcCommDetails) {
        imcCommDetails.setUpdateTime(DateUtils.getNowDate());
        return imcCommDetailsMapper.updateImcCommDetails(imcCommDetails);
    }

    /**
     * 批量删除佣金报主
     *
     * @param ids 需要删除的佣金报主ID
     * @return 结果
     */
    @Override
    public int deleteImcCommDetailsByIds(String[] ids) {
        return imcCommDetailsMapper.deleteImcCommDetailsByIds(ids);
    }

    /**
     * 删除佣金报主信息
     *
     * @param id 佣金报主ID
     * @return 结果
     */
    @Override
    public int deleteImcCommDetailsById(String id) {
        return imcCommDetailsMapper.deleteImcCommDetailsById(id);
    }

    /**
     * 查询佣金明细列表
     * @param commDetailsForm 代理人信息
     * @return
     */
    @Override
    public List<CommDetailsSearchVo> selectDetailsByBrokerInfo(CommDetailsForm commDetailsForm) {
        commDetailsForm.setTenantId(SecurityUtils.getTenantId());
        return imcCommDetailsMapper.selectDetailsByBrokerInfo(commDetailsForm);
    }

    //,{"titleText":"当月承保规保","title":"monthAcceptFirstPrem"}
    @Override
    public List<HashMap<String, String>> titles(Long commId) {
        List<HashMap<String, String>> resultList = new ArrayList<HashMap<String,String>>();
        ImcComm imcComm = imcCommMapper.selectImcCommById(commId);
        Long fundId = imcComm.getFundId();
        ImcFund imcFund = imcFundMapper.selectImcFundById(fundId);
        String profit1 = imcFund.getProfit1();
        if(!StringUtils.isNull(profit1)){
            HashMap<String, String> profit1Map = new HashMap<>();
            profit1Map.put("titleText",profit1);
            profit1Map.put("title","profit1");
            resultList.add(profit1Map);
        }

        String profit2 = imcFund.getProfit2();
        if(!StringUtils.isNull(profit2)){
            HashMap<String, String> profit2Map = new HashMap<>();
            profit2Map.put("titleText",profit2);
            profit2Map.put("title","profit2");
            resultList.add(profit2Map);
        }

        String profit3 = imcFund.getProfit3();
        if(!StringUtils.isNull(profit3)){
            HashMap<String, String> profit3Map = new HashMap<>();
            profit3Map.put("titleText",profit3);
            profit3Map.put("title","profit3");
            resultList.add(profit3Map);
        }

        String profit4 = imcFund.getProfit4();
        if(!StringUtils.isNull(profit4)){
            HashMap<String, String> profit4Map = new HashMap<>();
            profit4Map.put("titleText",profit4);
            profit4Map.put("title","profit4");
            resultList.add(profit4Map);
        }

        String profit5 = imcFund.getProfit5();
        if(!StringUtils.isNull(profit5)){
            HashMap<String, String> profit5Map = new HashMap<>();
            profit5Map.put("titleText",profit5);
            profit5Map.put("title","profit5");
            resultList.add(profit5Map);
        }

        String profit6 = imcFund.getProfit6();
        if(!StringUtils.isNull(profit6)){
            HashMap<String, String> profit6Map = new HashMap<>();
            profit6Map.put("titleText",profit6);
            profit6Map.put("title","profit6");
            resultList.add(profit6Map);
        }

        String profit7 = imcFund.getProfit7();
        if(!StringUtils.isNull(profit7)){
            HashMap<String, String> profit7Map = new HashMap<>();
            profit7Map.put("titleText",profit7);
            profit7Map.put("title","profit7");
            resultList.add(profit7Map);
        }

        String profit8 = imcFund.getProfit8();
        if(!StringUtils.isNull(profit8)){
            HashMap<String, String> profit8Map = new HashMap<>();
            profit8Map.put("titleText",profit8);
            profit8Map.put("title","profit8");
            resultList.add(profit8Map);
        }

        String profit9 = imcFund.getProfit9();
        if(!StringUtils.isNull(profit9)){
            HashMap<String, String> profit9Map = new HashMap<>();
            profit9Map.put("titleText",profit9);
            profit9Map.put("title","profit9");
            resultList.add(profit9Map);
        }

        String profit10 = imcFund.getProfit10();
        if(!StringUtils.isNull(profit10)){
            HashMap<String, String> profit10Map = new HashMap<>();
            profit10Map.put("titleText",profit10);
            profit10Map.put("title","profit10");
            resultList.add(profit10Map);
        }

        String profit11 = imcFund.getProfit11();
        if(!StringUtils.isNull(profit11)){
            HashMap<String, String> profit11Map = new HashMap<>();
            profit11Map.put("titleText",profit11);
            profit11Map.put("title","profit11");
            resultList.add(profit11Map);
        }

        String profit12 = imcFund.getProfit12();
        if(!StringUtils.isNull(profit12)){
            HashMap<String, String> profit12Map = new HashMap<>();
            profit12Map.put("titleText",profit12);
            profit12Map.put("title","profit12");
            resultList.add(profit12Map);
        }

        String profit13 = imcFund.getProfit13();
        if(!StringUtils.isNull(profit13)){
            HashMap<String, String> profit13Map = new HashMap<>();
            profit13Map.put("titleText",profit13);
            profit13Map.put("title","profit13");
            resultList.add(profit13Map);
        }

        String profit14 = imcFund.getProfit14();
        if(!StringUtils.isNull(profit14)){
            HashMap<String, String> profit14Map = new HashMap<>();
            profit14Map.put("titleText",profit14);
            profit14Map.put("title","profit14");
            resultList.add(profit14Map);
        }

        String profit15 = imcFund.getProfit15();
        if(!StringUtils.isNull(profit15)){
            HashMap<String, String> profit15Map = new HashMap<>();
            profit15Map.put("titleText",profit15);
            profit15Map.put("title","profit15");
            resultList.add(profit15Map);
        }

        return resultList;
    }

    @Override
    public List<CommDetailsTemplate> export(CommDetailsForm commDetailsForm) throws NoSuchFieldException, IllegalAccessException {
        //初始化导出模板
        makeCommDetailsInit();
        //先查询基本法 然后获得profit  对导出模板实体类进行修改
        List<HashMap<String, String>> titles = titles(commDetailsForm.getCommId());
        //整理表头
        HashMap<String,String> titleMap = getMapFromTitles(titles);
        //对实体类注解进行修改
        makeCommDetailsAnnotation(titleMap);

        ArrayList<CommDetailsTemplate> commDetailsTemplates = new ArrayList<>();
        List<CommDetailsSearchVo> commDetailsSearchVos = selectDetailsByBrokerInfo(commDetailsForm);
        for (CommDetailsSearchVo commDetailsSearchVo : commDetailsSearchVos) {
            CommDetailsTemplate commDetailsTemplate = new CommDetailsTemplate();
            BeanUtils.copyProperties(commDetailsSearchVo,commDetailsTemplate);
            commDetailsTemplates.add(commDetailsTemplate);
        }
        //再查询导出
        return commDetailsTemplates;
    }

    //将模板类恢复到初始状态
    private void makeCommDetailsInit() throws NoSuchFieldException, IllegalAccessException {
        Field field1 = CommDetailsTemplate.class.getDeclaredField("profit1");
        Field field2 = CommDetailsTemplate.class.getDeclaredField("profit2");
        Field field3 = CommDetailsTemplate.class.getDeclaredField("profit3");
        Field field4 = CommDetailsTemplate.class.getDeclaredField("profit4");
        Field field5 = CommDetailsTemplate.class.getDeclaredField("profit5");
        Field field6 = CommDetailsTemplate.class.getDeclaredField("profit6");
        Field field7 = CommDetailsTemplate.class.getDeclaredField("profit7");
        Field field8 = CommDetailsTemplate.class.getDeclaredField("profit8");
        Field field9 = CommDetailsTemplate.class.getDeclaredField("profit9");
        Field field10 = CommDetailsTemplate.class.getDeclaredField("profit10");
        Field field11 = CommDetailsTemplate.class.getDeclaredField("profit11");
        Field field12 = CommDetailsTemplate.class.getDeclaredField("profit12");
        Field field13 = CommDetailsTemplate.class.getDeclaredField("profit13");
        Field field14 = CommDetailsTemplate.class.getDeclaredField("profit14");
        Field field15 = CommDetailsTemplate.class.getDeclaredField("profit15");
        Field[] fields = new Field[]{field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12,field13,field14,field15};
        for (Field field : fields) {
            Excel excel = field.getAnnotation(Excel.class);//获取字段上的Excel注解
            InvocationHandler h = Proxy.getInvocationHandler(excel);
            Field hField = h.getClass().getDeclaredField("memberValues");
            hField.setAccessible(true);
            Map memberValues = (Map) hField.get(h);
            memberValues.put("type",Excel.Type.ALL);
            memberValues.put("name","默认字段");
            hField.setAccessible(false);
        }
    }

    /**
     * 佣金信息导入
     * @param file
     */
    @Override
    public void importDetails(MultipartFile file,Long commId) throws Exception{
        List<HashMap<String, String>> titles = titles(commId);
        //整理表头
        HashMap<String,String> titleMap = getMapFromTitles(titles);
        //对实体类注解进行修改
        makeCommDetailsAnnotation(titleMap);
        //设置模板
        ExcelUtil<CommDetailsTemplate> util = new ExcelUtil<CommDetailsTemplate>(CommDetailsTemplate.class);
        List<CommDetailsTemplate> detailsTemplates = util.importExcel(file.getInputStream());

        for (CommDetailsTemplate detailsTemplate : detailsTemplates) {
            ImcCommDetails imcCommDetails = new ImcCommDetails();
            BeanUtils.copyProperties(detailsTemplate,imcCommDetails);
            imcCommDetails.setUpdateTime(DateUtils.getNowDate());
            imcCommDetails.setUpdateId(SecurityUtils.getUsername());
            imcCommDetailsMapper.updateById(imcCommDetails);
        }

    }

    private HashMap<String, String> getMapFromTitles(List<HashMap<String, String>> titles) {
        HashMap<String, String> resultMap = new HashMap<>();
        for (HashMap<String, String> title : titles) {
            String key = title.get("title");
            String value = title.get("titleText");
            resultMap.put(key,value);
        }
        return resultMap;
    }

    private void makeCommDetailsAnnotation(HashMap<String, String> titles) throws NoSuchFieldException, IllegalAccessException {
        Field field1 = CommDetailsTemplate.class.getDeclaredField("profit1");
        Field field2 = CommDetailsTemplate.class.getDeclaredField("profit2");
        Field field3 = CommDetailsTemplate.class.getDeclaredField("profit3");
        Field field4 = CommDetailsTemplate.class.getDeclaredField("profit4");
        Field field5 = CommDetailsTemplate.class.getDeclaredField("profit5");
        Field field6 = CommDetailsTemplate.class.getDeclaredField("profit6");
        Field field7 = CommDetailsTemplate.class.getDeclaredField("profit7");
        Field field8 = CommDetailsTemplate.class.getDeclaredField("profit8");
        Field field9 = CommDetailsTemplate.class.getDeclaredField("profit9");
        Field field10 = CommDetailsTemplate.class.getDeclaredField("profit10");
        Field field11 = CommDetailsTemplate.class.getDeclaredField("profit11");
        Field field12 = CommDetailsTemplate.class.getDeclaredField("profit12");
        Field field13 = CommDetailsTemplate.class.getDeclaredField("profit13");
        Field field14 = CommDetailsTemplate.class.getDeclaredField("profit14");
        Field field15 = CommDetailsTemplate.class.getDeclaredField("profit15");
        Field[] fields = new Field[]{field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12,field13,field14,field15};
        for (Field field : fields) {
            String value = titles.get(field.getName());
            Excel excel = field.getAnnotation(Excel.class);//获取字段上的Excel注解
            InvocationHandler h = Proxy.getInvocationHandler(excel);
            Field hField = h.getClass().getDeclaredField("memberValues");
            hField.setAccessible(true);
            Map memberValues = (Map) hField.get(h);
            if(StringUtils.isEmpty(value)){//该基本法没用上该字段
                memberValues.put("type",Excel.Type.IMPORT);
            }else{
                memberValues.put("name",value);
            }
            hField.setAccessible(false);
        }
    }
}
